#include "frmCreateRoi.h"
#include <QFont>
#include <QSizePolicy>
#include <QIcon>
#include <QPixmap>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSpacerItem>

frmCreateRoi::frmCreateRoi(QWidget *parent) : QDialog(parent)
{
    setupUi();
}

void frmCreateRoi::setupUi()
{
    // 固定窗口大小和标题
    this->setWindowTitle(tr("创建ROI"));
    this->setFixedSize(1160, 655);
    this->setStyleSheet("background-color: rgb(220, 220, 220);");

    // 主网格布局
    gridLayout = new QGridLayout(this);
    gridLayout->setContentsMargins(5, 5, 5, 5);
    gridLayout->setSpacing(8);

    // 左侧 TabWidget 区域
    tabWidget = new QTabWidget(this);
    tabWidget->setMinimumSize(450, 550);
    tabWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    tabWidget->setStyleSheet("background-color: #bcc2bc;");
    tabWidget->setCurrentIndex(0);
    gridLayout->addWidget(tabWidget, 1, 0);

    // 右侧图像显示区，使用垂直布局
    imageLayout = new QVBoxLayout();
    imageLayout->setSpacing(8);
    gridLayout->addLayout(imageLayout, 1, 2);

    // tab - 基本设置
    tab = new QWidget();
    tabWidget->addTab(tab, tr("基本设置"));

    // tab 内部布局，用垂直布局组织3个分组和拉伸
    QVBoxLayout *tabLayout = new QVBoxLayout(tab);
    tabLayout->setContentsMargins(10, 10, 10, 10);
    tabLayout->setSpacing(20);

    // groupC1 - 图像对象
    groupC1 = new QGroupBox(tr("图像对象"), tab);
    groupC1->setStyleSheet(
            "QGroupBox {"
            " background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #dfd5cf, stop:0.15 #acbbac, stop:0.85 #acbbac, stop:1 #dfd5cf);"
            " color: rgb(85, 0, 255);"
            " font-weight: bold;"
            " border-radius: 8px;"
            "}"
    );

    // groupC1 布局
    QHBoxLayout *groupC1Layout = new QHBoxLayout(groupC1);
    groupC1Layout->setContentsMargins(10, 10, 10, 10);
    groupC1Layout->setSpacing(10);

    label_4 = new QLabel(tr("输入图像："), groupC1);
    label_4->setStyleSheet("color: rgb(30, 30, 30);");
    groupC1Layout->addWidget(label_4);

    txtLinkImage = new QLineEdit(groupC1);
    txtLinkImage->setStyleSheet("color: rgb(0, 0, 200); background-color: rgba(170, 170, 127,50);");
    txtLinkImage->setAlignment(Qt::AlignCenter);
    txtLinkImage->setReadOnly(true);
    txtLinkImage->setMinimumWidth(260);
    groupC1Layout->addWidget(txtLinkImage);

    btnLinkImage = new QPushButton(groupC1);
    btnLinkImage->setToolTip(tr("链接图像"));
    btnLinkImage->setStyleSheet(
            "QPushButton { border:none; border-radius: 8px; padding: 2px 4px; }"
            "QPushButton:hover { background-color: rgba(85, 150, 127, 100); }"
            "QPushButton:pressed { background-color: rgba(85, 180, 127, 160); }"
    );
    btnLinkImage->setIcon(QIcon(":/resource/link.png"));
    btnLinkImage->setIconSize(QSize(27, 27));
    groupC1Layout->addWidget(btnLinkImage);

    btnDelLinkImage = new QPushButton(groupC1);
    btnDelLinkImage->setToolTip(tr("删除图像链接"));
    btnDelLinkImage->setStyleSheet(
            "QPushButton { border:none; border-radius: 8px; padding: 2px 4px; }"
            "QPushButton:hover { background-color: rgba(226, 142, 162, 100); }"
            "QPushButton:pressed { background-color: rgba(226, 172, 162, 200); }"
    );
    btnDelLinkImage->setIcon(QIcon(":/resource/del_link.png"));
    btnDelLinkImage->setIconSize(QSize(27, 27));
    groupC1Layout->addWidget(btnDelLinkImage);

    tabLayout->addWidget(groupC1);

    // groupC2 - 轮廓对象
    groupC2 = new QGroupBox(tr("轮廓对象"), tab);
    groupC2->setStyleSheet(
            "QGroupBox {"
            " background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #dfd5cf, stop:0.15 #acbbac, stop:0.85 #acbbac, stop:1 #dfd5cf);"
            " color: rgb(85, 0, 255);"
            " font-weight: bold;"
            " border-radius: 8px;"
            "}"
    );

    QHBoxLayout *groupC2Layout = new QHBoxLayout(groupC2);
    groupC2Layout->setContentsMargins(10, 10, 10, 10);
    groupC2Layout->setSpacing(10);

    label_6 = new QLabel(tr("输入轮廓："), groupC2);
    label_6->setStyleSheet("color: rgb(30, 30, 30);");
    groupC2Layout->addWidget(label_6);

    txtLinkContour = new QLineEdit(groupC2);
    txtLinkContour->setToolTip("vector<vector<cv::Point>>类型");
    txtLinkContour->setStyleSheet("color: rgb(0, 0, 200); background-color: rgba(170, 170, 127,50);");
    txtLinkContour->setAlignment(Qt::AlignCenter);
    txtLinkContour->setReadOnly(true);
    txtLinkContour->setMinimumWidth(260);
    groupC2Layout->addWidget(txtLinkContour);

    btnLinkContour = new QPushButton(groupC2);
    btnLinkContour->setToolTip(tr("链接图像"));
    btnLinkContour->setStyleSheet(
            "QPushButton { border:none; border-radius: 8px; padding: 2px 4px; }"
            "QPushButton:hover { background-color: rgba(85, 150, 127, 100); }"
            "QPushButton:pressed { background-color: rgba(85, 180, 127, 160); }"
    );
    btnLinkContour->setIcon(QIcon(":/resource/link.png"));
    btnLinkContour->setIconSize(QSize(27, 27));
    groupC2Layout->addWidget(btnLinkContour);

    btnDelLinkContour = new QPushButton(groupC2);
    btnDelLinkContour->setToolTip(tr("删除图像链接"));
    btnDelLinkContour->setStyleSheet(
            "QPushButton { border:none; border-radius: 8px; padding: 2px 4px; }"
            "QPushButton:hover { background-color: rgba(226, 142, 162, 100); }"
            "QPushButton:pressed { background-color: rgba(226, 172, 162, 200); }"
    );
    btnDelLinkContour->setIcon(QIcon(":/resource/del_link.png"));
    btnDelLinkContour->setIconSize(QSize(27, 27));
    groupC2Layout->addWidget(btnDelLinkContour);

    tabLayout->addWidget(groupC2);

    // groupC3 - ROI类型
    groupC3 = new QGroupBox(tr("ROI类型"), tab);
    groupC3->setStyleSheet(
            "QGroupBox {"
            " background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #dfd5cf, stop:0.15 #acbbac, stop:0.85 #acbbac, stop:1 #dfd5cf);"
            " color: rgb(85, 0, 255);"
            " font-weight: bold;"
            " border-radius: 8px;"
            "}"
    );

    QHBoxLayout *groupC3Layout = new QHBoxLayout(groupC3);
    groupC3Layout->setContentsMargins(10, 10, 10, 10);
    groupC3Layout->setSpacing(10);

    label_5 = new QLabel(tr("创建的ROI类型："), groupC3);
    label_5->setStyleSheet("color: rgb(30, 30, 30);");
    groupC3Layout->addWidget(label_5);

    comboType = new QComboBox(groupC3);
    comboType->setStyleSheet(
            "QComboBox {"
            " border: 1px solid gray;"
            " border-radius: 2px;"
            " color: rgb(0, 0, 200);"
            " background-color: rgba(170, 170, 127,100);"
            "}"
            "QComboBox::drop-down {"
            " subcontrol-origin: padding;"
            " subcontrol-position: top right;"
            " width: 15px;"
            " border-left-width: 1px;"
            " border-left-color: darkgray;"
            " border-left-style: solid;"
            " border-top-right-radius: 3px;"
            " border-bottom-right-radius: 3px;"
            "}"
            "QComboBox::down-arrow {"
            " image: url(:/resource/down.png);"
            "}"
            "QComboBox::down-arrow:on {"
            " top: 1px;"
            " left: 1px;"
            "}"
    );
    comboType->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    comboType->addItem(tr("矩形"));
    comboType->addItem(tr("旋转矩形"));
    groupC3Layout->addWidget(comboType);

    tabLayout->addWidget(groupC3);

    // 底部执行区横向布局
    QHBoxLayout *horizontalLayout = new QHBoxLayout();
    horizontalLayout->setSpacing(10);
    horizontalLayout->setContentsMargins(0, 0, 0, 6);

    horizontalLayout->addSpacerItem(new QSpacerItem(10, 20, QSizePolicy::Fixed, QSizePolicy::Minimum));

    label_3 = new QLabel(tr("单元描述："));
    label_3->setStyleSheet("background-color: transparent;");
    horizontalLayout->addWidget(label_3);

    txtDescribe = new QLineEdit();
    txtDescribe->setMinimumSize(357, 30);
    horizontalLayout->addWidget(txtDescribe);

    horizontalLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Fixed, QSizePolicy::Minimum));

    btnExecute = new QPushButton(tr("执行"));
    btnExecute->setMinimumSize(90, 37);
    btnExecute->setStyleSheet(
            "QPushButton { background-color: rgb(85, 135, 127); color: rgb(235, 235, 235); border: 2px groove gray; border-radius: 8px; padding: 2px 4px; }"
            "QPushButton:hover { background-color: rgb(85, 155, 127); }"
            "QPushButton:pressed { background-color: rgb(85, 175, 127); }"
    );
    horizontalLayout->addWidget(btnExecute);

    horizontalLayout->addSpacerItem(new QSpacerItem(6, 20, QSizePolicy::Fixed, QSizePolicy::Minimum));

    // 添加到底部主布局，跨3列
    gridLayout->addLayout(horizontalLayout, 3, 0, 1, 3);

    // 边缘留白的间隔
    gridLayout->setRowMinimumHeight(0, 42);
    gridLayout->setRowMinimumHeight(2, 5);

    gridLayout->setColumnMinimumWidth(1, 8); // 左右间距中间列
}
